package net.zjitc.android2019.ch3storage;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import net.zjitc.android2019.R;

public class Ch3SQLiteActivity extends AppCompatActivity {
    Button btn_insert,btn_select,btn_update,btn_delete,btn_trans;
    MyHelper myHelper;
    SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ch3_sqlite);
        init();

        myHelper=new MyHelper(this);
        db=myHelper.getWritableDatabase();

        View.OnClickListener listener=new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String sql="";
                switch (v.getId()){
                    case R.id.ch3_sqlite_btn_insert:
                        /*
                        * sql="inser into user(name,sex) valuse(?,?)"
                        * db.execSQL(sql,new Object[]{"zhangsan","male"});
                        * */
                        ContentValues cv=new ContentValues();
                        cv.put("name","lisi");
                        cv.put("sex","female");
                        db.insert("user",null,cv);
                        Toast.makeText(Ch3SQLiteActivity.this,"添加成功",Toast.LENGTH_LONG).show();
                        break;
                    case R.id.ch3_sqlite_btn_delete:
                        /*sql="delete from user where id=?";
                        db.execSql(sql,new Object[]{2}):*/
                        db.delete("user","id=",new String[]{"1"});
                        Toast.makeText(Ch3SQLiteActivity.this,"删除成功",Toast.LENGTH_LONG).show();
                        break;
                    case R.id.ch3_sqlite_btn_update:
                        /*sql="update user set name=? where id=?";
                        db.execSQL(sql,new  Object[]{"Lily",4});*/
                        ContentValues cv1=new ContentValues();
                        cv1.put("name","lucy");
                        cv1.put("sex","female");
                        db.update("user",cv1,"id=?",new String[]{4+""});
                        Toast.makeText(Ch3SQLiteActivity.this,"更新成功",Toast.LENGTH_LONG).show();
                        break;
                    case R.id.ch3_sqlite_btn_select:
                        /*sql="select * from user";
                        Cursor cursor=db.rawQuery(sql,null);
                        while (cursor.moveToNext()){
                            String name=cursor.getString(cursor.getColumnIndex("name"));
                            String sex=cursor.getString(cursor.getColumnIndex("sex"));
                            Toast.makeText(Ch3SQLiteActivity.this,name+":"+sex,Toast.LENGTH_LONG).show();
                        }*/
                        Cursor cursor=db.query("uesr",null,null,null,null,null,null);
                        while (cursor.moveToNext()){
                            String name=cursor.getString(cursor.getColumnIndex("name"));
                            String sex=cursor.getString(cursor.getColumnIndex("sex"));
                            Toast.makeText(Ch3SQLiteActivity.this,name+":"+sex,Toast.LENGTH_LONG).show();
                        }
                        break;
                    case R.id.ch3_sqlite_btn_trans:
                        db.beginTransaction();
                        String sql1="update user set name=?";
                        String sql2="update user set sex=?";
                        try{
                            db.execSQL(sql1,new  Object[]{"zhm"});
                            finish();
                            db.execSQL(sql2,new  Object[]{"femal"});
                            db.setTransactionSuccessful();
                        }catch (Exception e){
                            Log.v("trans",e.getMessage());
                        }finally {
                            db.endTransaction();
                        }
                        break;
                }

            }
        };
        btn_insert=findViewById(R.id.ch3_sqlite_btn_insert);
        btn_select=findViewById(R.id.ch3_sqlite_btn_select);
        btn_update=findViewById(R.id.ch3_sqlite_btn_update);
        btn_delete=findViewById(R.id.ch3_sqlite_btn_delete);
        btn_trans=findViewById(R.id.ch3_sqlite_btn_trans);
    }

    private void init() {

    }
}
